package com.lms.dao;

import com.lms.bean.RentRecord;
import com.lms.bean.ReturnRecord;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ReturnRecordDao extends JpaRepository<ReturnRecord, Long>, JpaSpecificationExecutor<ReturnRecord> {

    @Query("select r from ReturnRecord r where r.user.username like ?1 and r.book.bookName like ?2")
    List<ReturnRecord> getList(String userName, String bookName, Pageable pageable);

    @Query("select r from RentRecord r where r.user.userId = ?1 and r.state = false and r.book.bookName like ?2 ")
    List<RentRecord> getReturnsList(Long userId,String bookName, Pageable pageable);

    @Query("select count(r) from ReturnRecord r where r.user.username like ?1 and r.book.bookName like ?2")
    Long countByBookNameAndUsername(String username, String bookname);

    @Modifying
    @Transactional
    @Query("delete  from ReturnRecord n where n.returnId in ?1")
    void deleteAll(List<Long> ids);

}
